package org.noworks.android.mvc.fragments;

import java.util.List;

import org.noworks.android.mvc.MvcApplication;
import org.noworks.android.mvc.activities.MvcSherlockFragmentActivity;
import org.noworks.android.mvc.exceptions.ActionNotFoundException;
import org.noworks.android.mvc.exceptions.ControllerNotFoundException;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockListFragment;

public class MvcSherlockListFragment<T> extends SherlockListFragment {

	private MvcApplication application; // Casted Application
	private MvcSherlockFragmentActivity activity; // Casted Activity
	private List<T> model;

	/**
	 * @param action
	 *            ActionMethod to call
	 * @throws ActionNotFoundException
	 *             Thrown when their is no ActionMethod for the given action
	 * @throws IllegalArgumentException
	 *             Thrown when their is no zero parameter method for the action
	 *             to be performed
	 */
	protected final void action(String action) {
		activity.action(action);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		activity = (MvcSherlockFragmentActivity) getActivity();
	}

	/**
	 * @param controller
	 *            Controller to call the ActionMethod in
	 * @param action
	 *            ActionMethod to call
	 * @throws ActionNotFoundException
	 *             Thrown when their is no ActionMethod for the given action
	 * @throws ControllerNotFoundException
	 *             Thrown when there is no controller found for the given
	 *             controller
	 */
	protected final void action(String controller, String action) {
		activity.action(controller, action);
	}

	/**
	 * @param action
	 *            ActionMethod to call
	 * @param args
	 *            Arguments to be passed to the ActionMethod
	 * @throws ActionNotFoundException
	 *             Thrown when their is no ActionMethod for the given action
	 * @throws IllegalArgumentException
	 *             Thrown when their is method with the given parameters for
	 *             method
	 */
	protected final void actionArguments(String action, Object... args) {
		activity.actionArguments(action, args);
	}

	/**
	 * @param controller
	 *            Controller to call the ActionMethod in
	 * @param action
	 *            ActionMethod to call
	 * @param args
	 *            Arguments to be passed to the ActionMethod
	 * @throws ActionNotFoundException
	 *             Thrown when their is no ActionMethod for the given action
	 * @throws ControllerNotFoundException
	 *             Thrown when there is no controller found for the given
	 *             controller
	 */
	protected final void actionArguments(String controller, String action,
			Object... args) {
		activity.actionArguments(controller, action, args);
	}

	public List<T> getModel() {
		return model;
	}

	/**
	 * Convenience method to the the MvcActivity
	 * 
	 * @return MvcActivity
	 */
	protected final MvcSherlockFragmentActivity getMvcActivity() {
		return activity;
	}

	/**
	 * Convenience method to get the MvcApplication
	 * 
	 * @return MvcApplication
	 */
	protected final MvcApplication getMvcApplication() {
		return application;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		Application app = activity.getApplication();
		application = (MvcApplication) app;
		this.activity = (MvcSherlockFragmentActivity) activity;
	}

	public MvcSherlockListFragment<T> setModel(List<T> model) {
		this.model = model;
		return this;
	}
}
